<template>
  <div class="login-container">
    <div class="box-card">
      <div class="avatar-box">
        <img src="../assets/img/logo.png">
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username" style="margin-top: 60px;">
          <el-input v-model="form.username" prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-s-goods" show-password></el-input>
        </el-form-item>
        <el-form-item class="button-box">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetFrom () {
      this.$refs.form.resetFields()
      this.form = {}
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data } = await this.$http.post('/login', this.form)
        if (data.meta.status === 200) {
          this.$message.success('登录成功')
          sessionStorage.setItem('token', data.data.token)
          this.$router.push('/home')
        } else {
          this.$message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../assets/img/login-bg.jpg') no-repeat;
  background-size: 100% 100%;
  .box-card {
    width: 450px;
    height: 360px;
    background-color: rgba(0,0,0,0.3);
    padding: 0 10px;
    position: relative;
    border-radius: 5px;
    animation: border 3s ease infinite;
    .avatar-box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -65%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .button-box {
      display: flex;
      justify-content: center;
      button {
        margin: 0 45px;
      }
    }
  }
  @keyframes border {
    0% {
      box-shadow: 0 0 7px #fff;
    }
    50% {
      box-shadow: none;
    }
    100% {
      box-shadow: 0 0 7px #fff;
    }
  }
}
</style>
